package com.et.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import unis.base.util.Log;
import unis.base.util.LogFactory;
import unis.base.util.ValidateUtil;

import com.et.pojo.CustomerInfo;
import com.opensymphony.xwork2.ActionSupport;


public abstract class GenericAction extends ActionSupport {
	protected String method = "";
	protected String defaultEncoding = "UTF-8";
	protected final Log LOGGER = LogFactory.getServerLog(getClass());
	
	
	/**
	 * 获取缓存当前登录用户信息
	 */
	protected CustomerInfo getUser(){
		return (CustomerInfo)getSession().getAttribute("customerInfo");
	}
	
	/**
	 * 获取缓存当前登录用户ID
	 */
	protected int getUserId(){
		CustomerInfo customerInfo = (CustomerInfo)getSession().getAttribute("customerInfo");
		if(ValidateUtil.isNotNull(customerInfo)){
			return customerInfo.getCustomerID();
		}else{
			return 0;
		}
	}
	/**
	 * 返回当前HttpSession的Id
	 * 
	 * @return Session的Id
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 将指定的名称和值在HttpSession中绑定
	 * 
	 * @param name 绑定的名称
	 * @param value 值
	 */
	protected void setSessionAttribute(String name, Object value) {
		getSession().setAttribute(name, value);
	}

	/**
	 * 返回HttpSession中绑定名称的值
	 * 
	 * @param name 绑定的名称
	 * @return 绑定名称的值
	 */
	protected Object getSessionAttribute(String name) {
		return getSession().getAttribute(name);
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	
	/**
	 * response对text文档的输出  常用语ajax方法
	 * @param text 输出的text字符串
	 */
	public void outPrintText(String text){
		HttpServletResponse response = this.getResponse();
		response.setContentType("text/plain; charset=utf-8");
		try {
			response.getWriter().write(text);
			response.getWriter().close();
			response.getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
